package com.naiterui.ehp.bs.ds.modules.sys.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.ds.common.constants.CacheConstants;
import com.naiterui.ehp.bs.ds.common.entity.BaseTree;
import com.naiterui.ehp.bs.ds.modules.sys.entity.Dict;
import com.naiterui.ehp.bs.ds.modules.sys.mapper.DictMapper;
import com.naiterui.ehp.bs.ds.modules.sys.service.IDictService;
import com.naiterui.ehp.bs.ds.modules.sys.vo.DictVO;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Override
    @CacheEvict(value = CacheConstants.DICT, allEntries = true, key = "#entity.type")
    public boolean save(Dict entity) {
        return super.save(entity);
    }

    @Override
    @Cacheable(value = CacheConstants.DICT, key = "#type")
    public List<DictVO> queryVueList(String type) {
        return this.baseMapper.getByType(type);
    }

    @Override
    public List<Dict> queryList() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_at");
        queryWrapper.orderByAsc("order_num");
        List<Dict> dicts = super.list(queryWrapper);
        return BaseTree.listToTree(dicts);
    }

    @Override
    @CacheEvict(value = CacheConstants.DICT, allEntries = true, key = "#entity.type")
    public boolean updateById(Dict entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(value = CacheConstants.DICT, allEntries = true)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

}
